package com.x.Part1_encapsulation;

/**
 * ClassName: Encapsulation
 * Package: IntelliJ IDEA
 * Description:
 * Written by HanXu
 * Create 2023/9/22 1:50
 */
public class Encapsulation {
    public static void main(String[] args) {
        Animal a1 = new Animal();

        //private属性 在Animal类的外部不可见
        //a1.name = "Bug";
        //a1.legNum = 8;

        //private属性只能通过set方法赋值，通过get方法获取
    }
}

class Animal{
    private String name;
    private int legNum;

    public void eat(){
        System.out.println("Eat");
    }

    public void setLegNum(int num){
        if (num >= 0 && num%2 == 0)
            legNum = num;
        else
            System.out.println("你输入的数值非法");
    }

    public void setName(String str){
        name = str;
    }
}